﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SureKAM.BackUp.Notify.Domain;
using SureKAM.BackUp.Notify.Presenter;
using SureKAM.BackUp.Notify.Presenter.Interfaces;
using SureKAM.BackUp.Notify.Presenter.ViewInterfaces;
using SureKAM.BackUp.Notify.Service;

namespace SureKAM.BackUp.Notify.WinForm
{
    public partial class DirectoryWin : Form, IDirectoryWinView
    {
        private IDirectoryWinPresenter directoryWinPresenter;
        public DirectoryWin()
        {
            InitializeComponent();
        }

        private void DiectoryManage_Load(object sender, EventArgs e)
        {
            this.dataGirdDirectory.AutoGenerateColumns = false;
            this.directoryWinPresenter = new DirectoryWinPresenter(this, new DirectoryService());
            this.directoryWinPresenter.Init();
        }

        private void close_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        /// <summary>
        /// 验证文本框
        /// </summary>
        private bool VildateTextBox()
        {
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()) || string.IsNullOrEmpty(this.txtMaxSize.Text.Trim()) || string.IsNullOrEmpty(this.txtBackupServer.Text.Trim()) || string.IsNullOrEmpty(this.txtAddress.Text.Trim()))
            {
                MessageBox.Show("请填写相关信息！");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 添加目录对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void add_Click(object sender, EventArgs e)
        {
            if (this.VildateTextBox())
            {
                if (this.directoryAdd(this.txtName.Text.Trim(), Convert.ToInt32(this.txtMaxSize.Text.Trim()), this.txtBackupServer.Text.Trim(), this.txtAddress.Text.Trim()))
                {
                    ////添加成功
                    this.ReflashDataBind();
                    this.txtName.Text = "";
                    this.txtMaxSize.Text = "";
                    this.txtBackupServer.Text = "";
                    this.txtAddress.Text = "";
                }
                else
                {
                    ////添加失败
                    MessageBox.Show("添加失败!");
                }
            }
        }

        /// <summary>
        /// 绑定DataGird
        /// </summary>
        /// <param name="directories"></param>
        public void DataBindDirectiry(List<Domain.Directory> directories)
        {
            if (directories != null)
            {
                this.dataGirdDirectory.DataSource = directories;
            }
        }

        private void  ReflashDataBind()
        {
            this.dataGirdDirectory.DataSource = null;
            this.dataGirdDirectory.DataSource = this.reflash();
        }

        /// <summary>
        /// 删除选中对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delete_Click(object sender, EventArgs e)
        {
            string currentId = this.dataGirdDirectory.Rows[this.dataGirdDirectory.CurrentCell.RowIndex].Cells[0].Value.ToString();
            if(!string.IsNullOrEmpty(currentId))
            {
                directoryDelete(currentId);
                this.ReflashDataBind();
            }
        }

        /// <summary>
        /// 改变数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGirdDirectory_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                this.txtName.Text = this.dataGirdDirectory.Rows[this.dataGirdDirectory.CurrentCell.RowIndex].Cells[1].Value.ToString();
                this.txtMaxSize.Text = this.dataGirdDirectory.Rows[this.dataGirdDirectory.CurrentCell.RowIndex].Cells[4].Value.ToString();
                this.txtBackupServer.Text = this.dataGirdDirectory.Rows[this.dataGirdDirectory.CurrentCell.RowIndex].Cells[2].Value.ToString();
                this.txtAddress.Text = this.dataGirdDirectory.Rows[this.dataGirdDirectory.CurrentCell.RowIndex].Cells[5].Value.ToString();
            }
            catch (Exception)
            {
                this.ReflashDataBind();
            }
            
        }
        
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void edit_Click(object sender, EventArgs e)
        {
           string current= this.dataGirdDirectory.Rows[this.dataGirdDirectory.CurrentCell.RowIndex].Cells[0].Value.ToString();
            if(!String.IsNullOrEmpty(current))
            {
                Notify.Domain.Directory directory=new Directory();
                directory.ID = current;
                directory.Name = this.txtName.Text;
                directory.MaxSize = Convert.ToInt32(this.txtMaxSize.Text);
                directory.BackUpServer = this.txtBackupServer.Text;
                directory.Address = this.txtAddress.Text;
                if(directoryUpdate(directory))
                {
                    this.ReflashDataBind();
                }
            }
        }


        public DirectiryAdd directoryAdd { get; set; }
        public flashList reflash { get; set; }
        public DirectoryDelete directoryDelete{get; set; }
        public DirectoryUpdate directoryUpdate{get; set; }
    }
}
